package com.example.demo.design.state_pattern.simple.account;

/**
 * @Author: liull
 * @Description: 账户类 环境类
 * @Date: Created in 19:38 2020/11/24
 * @Modified By:
 */

public class Account {

    /** 抽象状态 */
    private AccountState state;

    /** 开户名 */
    private String owner;

    /** 账户余额 */
    private double balance = 0;

    public Account(String owner, double balance) {
        this.owner = owner;
        this.balance = balance;
        // 设置初始状态
        this.state = new NormalState(this);
        System.out.println(this.owner + "开户，初始金额为" + balance);
        System.out.println("-----------------------------------------");
    }

    public double getBalance() {
        return this.balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    public void setState(AccountState state) {
        this.state = state;
    }

    public void deposit(double amount) {
        System.out.println(this.owner + "存款" + amount);
        state.deposit(amount); // 调用状态对象的deposit()方法
        System.out.println("现在余额为" + this.balance);
        System.out.println("现在帐户状态为" + this.state.getClass().getName());
        System.out.println("---------------------------------------------");
    }

    public void withdraw(double amount) {
        System.out.println(this.owner + "取款" + amount);
        state.withdraw(amount); // 调用状态对象的withdraw()方法
        System.out.println("现在余额为" + this.balance);
        System.out.println("现在帐户状态为" + this.state.getClass().getName());
        System.out.println("---------------------------------------------");
    }

    public void computeInterest() {
        state.computeInterest(); // 调用状态对象的computeInterest()方法
    }
}
